from . import Commondef


class RoomRule(object):
    """docstring for RoomRule"""

    def __init__(self, arg):
        self.arg = arg


class Pair(object):
    """docstring for QueTou"""

    def __init__(self, tile1, tile2):
        self.tile1 = tile1
        self.tile2 = tile2

    def IsValid(self):
        ''' IsValid '''
        return self.tile1 == self.tile2


class Meld(object):
    """docstring for className"""

    def __init__(self, tile1, tile2, tile3):
        self.tile1 = tile1
        self.tile2 = tile2
        self.tile3 = tile3

    def IsValid(self):
        return self.IsSequence() or self.IsTriplets()
        # if self.IsSequence() or self.IsTriplets():
        #     return True
        # else:
        #     return False
    # def IsValid(self):
    # 	if (self.tile1.type == self.tile2.type and
    # 		self.tile2.type == self.tile3.type):
    # 		if(self.tile1.rank == self.tile2.rank -1 and
    # 			self.tile2.rank == self.tile3.rank - 1):
    # 			return True
    # 		elif(self.tile1.rank == self.tile2.rank and
    # 			self.tile2.rank == self.tile3.rank ):
    # 			return True
    # 	else:
    # 		return False
    #

    def IsSequence(self):
        """ IsSequence"""
        if (self.tile1.type == self.tile2.type and
                self.tile2.type == self.tile3.type):
            if(self.tile1.rank == self.tile2.rank - 1 and
                    self.tile2.rank == self.tile3.rank - 1):
                return True
        else:
            return False

    def IsTriplets(self):
        if self.tile1 == self.tile2 == self.tile3:
            return True
        else:
            return False


class MahJongRule(object):
    """docstring for MahJongRule"""

    def __init__(self, rule):
        self.rule = rule


def is_test(a, b):
    """kdsfasdf

    :arg1: TODO
    :returns: TODO

    """
    pass


def IsHandHu(hand, pairNeeded=Commondef.PAIR_MAX,
             tripNeeded=Commondef.TRIPLETS_MAX):
    """IsHandHu"""
    pairList = FindHandPairs(hand)
    for pair in pairList:
        hand_copy = hand[:]
        hand_copy.remove(pair)
        hand_copy.remove(pair)
        if IsAllMeld(hand_copy):
            return True
        else:
            pass
    return False


def FindHandPairs(hand):
    """FindHandPairs"""
    pairList = []
    setHand = list(set(hand))
    setHand.sort()
    for tile in setHand:
        if hand.count(tile) > 1:
            pairList.append(tile)
    return pairList


MELB_COUNT = 3


def IsAllMeld(hand):
    if len(hand) == 3 or len(hand) == 0:
        return Meld(*hand).IsValid()
    elif len(hand) < 3:
        return False
    setHand = list(set(hand))
    setHand.sort()
    handIter = hand[:]
    for tile in setHand:
        if handIter.count(tile) >= 3:
            handIter.remove(tile)
            handIter.remove(tile)
            handIter.remove(tile)
            return IsAllMeld(handIter)
        elif handIter.count(tile) < 3:
            allInsect = GetAllInsect(setHand, handIter.index(tile), MELB_COUNT)
            for insect in allInsect:
                if Meld(*insect).IsValid():
                    for tile in insect:
                        handIter.remove(tile)
                    return IsAllMeld(handIter)

        else:
            continue

    return False


def IsAllMeldFast1(hand):
    if len(hand) == 3:
        return Meld(*hand).IsValid()
    elif len(hand) < 3:
        return False
    setHand = list(set(hand))
    setHand.sort()
    handIter = hand[:]
    tile = handIter[0]
    if handIter.count(tile) >= 3:
        handIter.remove(tile)
        handIter.remove(tile)
        handIter.remove(tile)
        return IsAllMeld(handIter)
    else:
        headThree = setHand[0:3]
        if Meld(*headThree).IsSequence():
            for tile in headThree:
                handIter.remove(tile)
            return IsAllMeld(handIter)
        else:
            return False
    return False


def GetAllInsect(hand, indexNeed, count):
    allInsect = []
    firstIndex = indexNeed - count + 1
    lastIndex = indexNeed
    if lastIndex + count > len(hand) - 1:
        lastIndex = len(hand) - count - 1
    if firstIndex < 0:
        firstIndex = 0
    for index in range(firstIndex, lastIndex + 1):
        allInsect.append(hand[index:index + count])
    return allInsect


def GetKeyTile(hand):
    pass


def GetSortLeft(hand):
    '''
    11123456789999m


    valueLeft = valueHand.del(LargestMerds)
    left = valueLeft + noneValueTiles
    valueLeftSingle = GetNoneValueTiles(valueLeft)
    minSetps = GetHuSetps(left)



    for MerdGroup in All MerdPos:
            valueHandTemp = valueHand[:]
            left = valueHandTemp.del(MerdGroup)

    '''
    noneValueTiles = GetNoneValueTiles(hand)
    handTemp = hand[:]

    for tile in noneValueTiles:
        handTemp.remove(tile)
    valueHand = handTemp
    LargestMerds, valueLeft = GetLargetMerds(valueHand)
    valueLeftSingle = GetNoneValueTiles(valueLeft)
    left = noneValueTiles
    left[len(noneValueTiles):] = valueLeftSingle
    return left


def GetLargetMerds(hand):
    """[summary]

    [description]

    Arguments:
            hand {[type]} -- [description]
    """

    handIter = hand[:]
    canTry = True
    merds = []

    while canTry is True:
        setHand = list(set(handIter))
        setHand.sort()
        canTry = False
        for index in xrange(0, len(setHand)):
            tile = handIter[index]
            if handIter.count(tile) >= 3:
                handIter.remove(tile)
                handIter.remove(tile)
                handIter.remove(tile)
                merds.append(tile)
                merds.append(tile)
                merds.append(tile)
                canTry = True
                break
            elif index + 2 < len(setHand) - 1:
                headThree = setHand[index:index + 3]
                if Meld(*headThree).IsSequence():
                    for tile in headThree:
                        handIter.remove(tile)
                        merds.append(tile)
                    canTry = True
                    break

    return merds, handIter


def GetNextEliment(hand, index):
    adjustIndex = index - 1
    if adjustIndex < 0:
        adjustIndex = 0
    NextEliment = hand[adjustIndex:index + 2]
    NextEliment.remove(hand[index])
    return NextEliment


def GetNoneValueTiles(hand):
    singles = []
    setHand = list(set(hand))
    setHand.sort()
    handIter = hand[:]
    for i in xrange(0, len(setHand)):
        tile = setHand[i]
        if handIter.count(tile) == 1:
            adjTiles = GetNextEliment(setHand, i)
            bValue = False
            for adjTile in adjTiles:
                bValue = tile.IsValueTo(adjTile)
            if bValue is False:
                singles.append(tile)
    return singles


def GetAllMerdPossible(hand):
    pass
